﻿$(function () {

    initDate_Tzgg();

    //控件初始化及初始数据加载
    initTzggControl();

    //按钮及事件
    registTzggEvent();
})

function initDate_Tzgg() {
    //$('#s_dateTzggCJSJBgn').val(getStartDate());
    //$('#s_dateTzggCJSJEnd').val(getEndDate());

}

// 控件初始化及初始数据加载
function initTzggControl() {

    // 可评查案件列表DataGrid初始化
    $('#gridTzggList').datagrid({
        fitColumns: true,
        striped: true,
        singleSelect: false,
        checkOnSelect: false,
        loadMsg: '数据加载中，请稍后...',
        pagination: true,
        rownumbers: true,
        fit:true,
        idField: 'BH',
        toolbar: $('#divTzggSearch'),
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        columns: [[
            {field: 'BH', title: '唯一标示', hidden: true },
            { field: 'BT', title: '标题', width: 150 },
            { field: 'NR', title: '内容', width: 250 },
            { field: 'GHSJ',formatter:function (value,row,index) {
                return sjzh(value)
            }, title: '公告截至日期', width: 100 },
            { field: 'SFJYKJ', title: '是否下级院可见', width: 120 ,
                formatter: function(value,row,index){
                    if (row.SFJYKJ == 'Y'){
                        return '是';
                    } else {
                        return '否';
                    }
                }
            },
            { field: 'CJSJ',formatter:function (value,row,index) {
                return sjzh(value)
            }, title: '发布时间', width: 150 },

            { field: 'FBRXM', title: '发布人姓名', width: 80 },
            { field: 'FBRDWBM', title: '发布单位编码', hidden: true },
            { field: 'action', title: '操作', width: 120, align: 'center',
                formatter: function (value, row, index) {
                    var e = '<a href="#" onclick="show_notice_window(' + row.BH + ')">查看</a> ';
                    var d = '<a href="#" onclick="updateTzgg(' + index + ')">修改</a> ';
                    var f = '<a href="#" onclick="deleteTzgg(' + index + ')">删除</a>';
                    return e + d + f;
                }
            }
        ]],
        onClickRow: function (rowIndex, rowData) {
            $('#gridTzggList').datagrid('clearSelections');
            $('#gridTzggList').datagrid('highlightRow', rowIndex);
        },
        onLoadSuccess: function () {
        }
    });
    // 分页控件(中文)
    $('#gridTzggList').datagrid('getPager').pagination({
        beforePageText: '第',
        afterPageText: '页   共{pages}页',
        displayMsg: '当前显示【{from} ~ {to}】条记录   共【{total}】条记录'
    });

    getTzggDataSource();

    // 页面分页控件放置到底部
    resizeTzggListHeight();

}

function resizeTzggListHeight() {
     /*  $('#ttTzgg').tabs('options').height = $('#tt').tabs('options').height - 30;
       $('#ttTzgg').tabs('resize');

    var h = $('#divTzggDataList').height();
    $('#gridTzggList').datagrid('options').height = h;
    $('#gridTzggList').datagrid('resize');*/
   /* var height = $('#panel_tzgg').height();
    var h = $('#layout_tzgg').height();
    var width = $('#panel_tzgg').width();
    $('#gridTzggList').datagrid('options').width = width - 4;
    $('#gridTzggList').datagrid('options').height = height - h - 6;
    $('#gridTzggList').datagrid('resize');*/


}

// 按钮及事件
function registTzggEvent() {
    // 查询
    $('#btnTzggSearch').linkbutton({

        onClick: function () {
            // 查询数据
            getTzggDataSource();
        }
    });

    // 发布
    $('#btnTzggAssign').linkbutton({

        onClick: function () {
            insertTzgg();
        }
    });
}

// 根据查询条件，获取可评查案件列表
function getTzggDataSource() {
    var obj = new Object();
    obj.KeyWord = $("#keyword_announce").val();
    obj.FBSJBeg = $('#s_dateTzggCJSJBgn').datebox('getValue');
    obj.FBSJEnd =  $('#s_dateTzggCJSJEnd').datebox('getValue');

    // 调用一般处理程序获取数据
    $('#gridTzggList').datagrid({
        url: getRootPath()+'/resource/getTzgg',
        queryParams: { json : JSON.stringify(obj)}
    });
}

//发布通知公告
function insertTzgg() {

    commonTzgg();

    // 初始化输入框
    init_win_resource_tzgg(null, false);

    // 发布
    $('#btnTzggConfirm').linkbutton({
        iconCls: 'icon-ok',
        disabled: false,
        text: '发布',
        onClick: function () {
            //上传文件
            var bt = valiate($('#txtTzggBT').textbox('getValue'));
            if (bt == undefined || bt == "") {
                Alert("请添加标题。");
                return;
            }
            var nr = valiate($('#txt_tzgg_nr').textbox('getValue'));
            if (nr == undefined || nr == "") {
                Alert("请添加内容。");
                return;
            }
            var sfjykj = $("input[name='sfjykj_tzgg'][checked]").val();
            var ghsj = $('#date_tzgg').datebox('getValue');

            var obj = new Object();
            obj.bt = bt;
            obj.nr = nr;
            obj.ghsj = ghsj;
            obj.sfjykj = sfjykj;

            $.ajax({
                // url: getRootPath() + "/resource/UpdTzgg",
                url: getRootPath() + "/notice/saveNotice",
                data: JSON.stringify(obj),
                type: 'post',
                async: false,
                dataType: 'json',
                contentType:"application/json",
                success: function (result) {
                    debugger
                    if (result.code == 200) {
                        getTzggDataSource();
                        Alert('发布成功！');
                        tzgg_fileUplpload(result.data.bh);
                        $('#winTzggAssign').window('close');
                    } else {
                        Alert('发布失败');
                    }
                }
            });
        }
    });
    // 弹出分配框
    $('#winTzggAssign').window({
        title: '通知公告【添加】'
    });
    $('#winTzggAssign').window('open');
}

//初始化通知公告弹出界面
function commonTzgg() {
    // 取消
    $('#btnTzggCancel').linkbutton({
        iconCls: 'icon-cancel',
        onClick: function () {
            $('#winTzggAssign').window('close');
        }
    });
}

//查看通知公告
function viewTzgg(index) {
    commonTzgg();

    var rowDatas = $('#gridTzggList').datagrid('getRows');
    // 初始化输入框
    init_win_resource_tzgg(rowDatas[index], true);

    $('#btnTzggConfirm').linkbutton({
        iconCls: 'icon-ok',
        disabled: true
    });

    $('#winTzggAssign').window({
        title: '通知公告【查看】'
    });
    $('#winTzggAssign').window('open');
}

//修改通知公告
function updateTzgg(index) {
    commonTzgg();

    var rowDatas = $('#gridTzggList').datagrid('getRows');
    // 初始化输入框
    init_win_resource_tzgg(rowDatas[index], false);
    var bh = rowDatas[index].BH;

    $('#btnTzggConfirm').linkbutton({
        iconCls: 'icon-ok',
        disabled: false,
        text: '确定',
        onClick: function () {
            var bt = valiate($('#txtTzggBT').textbox('getValue'));
            if (bt == undefined || bt == "") {
                Alert("请添加标题。");
                return;
            }
            var nr = $('#txt_tzgg_nr').val();
            if (nr == undefined || nr == "") {
                Alert("请添加内容。");
                return;
            }
            if($('#tzgg_zlmc').textbox("getValue") == ""){
                Alert("请输入文件名称！");
                return;
            }
            var sfjykj = $("input[name='sfjykj_tzgg'][checked]").val();
            var ghsj = $('#date_tzgg').datebox('getValue');

            var obj = new Object();
            obj.bt = bt;
            obj.nr = nr;
            obj.ghsj = ghsj;
            obj.sfjykj = sfjykj;

            $.ajax({
                // url: getRootPath() + "/resource/UpdTzgg",
                url: getRootPath() + "/notice/saveNotice",
                data: JSON.stringify(obj),
                type: 'post',
                async: false,
                contentType:"application/json",
                dataType: 'json',
                success: function (result) {
                    debugger
                    if (result.code == 200) {
                        getTzggDataSource();
                        Alert('修改成功！');
                        tzgg_fileUplpload(result.data.bh);
                        $('#winTzggAssign').window('close');
                    } else {
                        Alert('修改失败');
                    }
                }
            });
        }
    });

    $('#winTzggAssign').window({
        title: '通知公告【修改】',

    });
    $('#winTzggAssign').window('open');
}

// 删除通知公告
function deleteTzgg(index) {
    Confirm("确认", "确定删除？", function (r) {
        if (r) {
            var rowDatas = $('#gridTzggList').datagrid('getRows');
            var bh = rowDatas[index].BH;

            $.ajax({
                url: getRootPath() + "/resource/delTzgg",
                data: { bh: bh },
                type: 'post',
                async: true,
                dataType: 'json',
                success: function (result) {
                    if (result.status == 200) {
                        getTzggDataSource();
                        Alert('删除成功！');
                    } else {
                        Alert('删除失败！');
                    }
                }
            });
        }
    });
}

// 数据验证
function valiate(data) {
    return (data == undefined) ? "" : data;
}

// 设置通知公告输入框初始值及只读属性
function init_win_resource_tzgg(data, readonly) {

    // 绑定数据
    if (data){
        $('#txtTzggBT').textbox('setText', data.BT);
        //$('#txt_tzgg_nr').val(data.NR.replace(/<br>/g,'\n'));
        $('#txt_tzgg_nr').textbox('setText', data.NR);
        $('#date_tzgg').datebox('setValue', data.GHSJ);
        if (data.SFJYKJ == 'Y'){
            $("input[name='sfjykj_tzgg'][value=Y]").attr("checked", true);
        }else{
            $("input[name='sfjykj_tzgg'][value=N]").attr("checked", true);
        }
    } else {
        $('#txtTzggBT').textbox('setText', "");
        $('#txt_tzgg_nr').textbox('setText', "");
        $('#date_tzgg').datebox('setValue', "");
        //$('#date_tzgg').val(getNowDate);
        $("input[name='sfjykj_tzgg'][value=N]").attr("checked", true);
    }

    // 设置只读属性
    $('#txtTzggBT').textbox({ disabled: readonly });
    $('#txt_tzgg_nr').attr('disabled', readonly);
    $('#date_tzgg').datebox('readonly', readonly);
    $("input[name='sfjykj_tzgg']").attr("disabled", readonly);
}

//获取上传文件的名称并设置
$("#tzgg_upload_file").change(function (){
    var file_val = $("#tzgg_upload_file").val();
    var index = file_val .lastIndexOf("\\");
    file_val  = file_val .substring(index + 1, file_val .length);
    $("#tzgg_zlmc").textbox("setValue",file_val);
    // event.target.value='';
});
//点击上传
function tzgg_fileUplpload(bh) {
    var path=getRootPath()+'/notice/saveNoticeFile';
    var formData=new FormData($("#fileUpTZ")[0]);
    formData.append( "bh", bh );
    $.ajax({
        url:path,
        type:'POST',
        data:formData,
        async:false,
        cache:false,
        contentType:false,
        dataType:"json",
        processData:false,
        success:function (data) {

        },
        error: function (data) {
            Alert('上传文件失败');
        }

    })
}